//客观分
//距敌军集结点距离，越近加分越高(总分30)
//本身交通，交通越好加分越高(总分10)
//本身防御，防御越好加分越高(总分10)
//目标点周围军队数量，数量越少加分越高(总分10)

//主观分
//距当前点距离，越近加分越高(总分40)


var AISituation = {
	GetForcePointMap : function(forceid, tgtforceid){
		//距敌军集结点距离，越近加分越高
		var forcePointMap = tileEngine.MapPay(forceInfo[forceid].pass_index, campaign.force[tgtforceid].tileIndex, 5000);
		var maxforcePoint = 0;
		for (var tileIndex = 0; tileIndex < forcePointMap.length; tileIndex++){
			pay = forcePointMap[tileIndex].pay;
			if (pay >= 0 && pay > maxforcePoint)
				maxforcePoint = pay;
		}
		for (var tileIndex = 0; tileIndex < forcePointMap.length; tileIndex++){
			pay = forcePointMap[tileIndex].pay;
			forcePointMap[tileIndex] = pay >= 0 ?(1 - (forcePointMap[tileIndex].pay / maxforcePoint)) : pay;
		}
		return forcePointMap;
	},
	
	GetPassMap : function(forceid, tgtforceid){
		//本身交通，交通越好加分越高
		var passMap = DeepClone(forceInfo[forceid].pass_index);
		var maxpass = 0;
		for (var tileIndex = 0; tileIndex < passMap.length; tileIndex++){
			if (passMap[tileIndex] >= 0 && passMap[tileIndex] > maxpass)
				maxpass = passMap[tileIndex];
		}
		for (var tileIndex = 0; tileIndex < passMap.length; tileIndex++){
			passMap[tileIndex] = passMap[tileIndex] >= 0 ?(1 - (passMap[tileIndex] / maxpass)) : passMap[tileIndex];
		}
		return passMap;
	},
	
	//本身防御，防御越好加分越高
	GetDefendMap : function(forceid, tgtforceid){
		var defendMap = DeepClone(forceInfo[forceid].defend_index);
		var maxdefend = 0;
		for (var tileIndex = 0; tileIndex < defendMap.length; tileIndex++){
			if (defendMap[tileIndex] >= 0 && defendMap[tileIndex] > maxdefend)
				maxdefend = defendMap[tileIndex];
		}
		for (var tileIndex = 0; tileIndex < defendMap.length; tileIndex++){
			defendMap[tileIndex] = defendMap[tileIndex] >= 0 ?(defendMap[tileIndex] / maxdefend) : defendMap[tileIndex];
		}
		return defendMap;
	},
	
	//目标点周围军队数量，数量越少加分越高(总分10),
	//一只部队所在位置为1，周围六个格子拥挤值为0.5，每个格子最多三只部队，一个格子最大拥挤值为 12
	GetTroopCrowMap : function(forceid, tgtforceid){
		var seeMap = DeepClone(forceInfo[forceid].seeMap);
		var troopcrowMap = CreateArray(0, seeMap.length, 1, false);
		for (var troopid = 0; troopid < campaign.troop.length; troopid++){
			var tileIndex = campaign.troop[trooopid].tileIndex;
			if (seeMap[tileIndex]){
				troopcrowMap[tileIndex] -= (1 / 12);
				for (var neighborid in tileEngine.neighborCache[tileIndex]){
					troopcrowMap[tileEngine.neighborCache[tileIndex][neighborid]] -= (0.5 / 12);
				}
			}
		}
		return troopcrowMap;
	},

	//客观地图，可整个势力公用的
	CalcObjectMap : function(forcePointMap, passMap, defendMap, troopcrowMap){
		var objectMap = CreateArray(0, tileEngine.tileCount, 0, false);
		for (var tileIndex = 0; tileIndex < objectMap.length; tileIndex++){
			if (forcePointMap[tileIndex] >= 0)
				objectMap[tileIndex] += 30 * forcePointMap[tileIndex];
			if (passMap[tileIndex] >= 0)
				objectMap[tileIndex] += 10 * passMap[tileIndex];
			if (defendMap[tileIndex] >= 0)
				objectMap[tileIndex] += 10 * defendMap[tileIndex];
			if (troopcrowMap[tileIndex] >= 0)
				objectMap[tileIndex] += 10 * troopcrowMap[tileIndex];
		}
		return objectMap;
	},
	
	//距当前点距离，越近加分越高
	GetCurrentDistMap : function(troopTileId, forceid, tgtforceid){
		var distMap = tileEngine.MapPay(forceInfo[forceid].pass_index, troopTileId, 5000);
		var maxdist = 0;
		for (var tileIndex = 0; tileIndex < distMap.length; tileIndex++){
			pay = distMap[tileIndex].pay;
			if (pay >= 0 && pay > maxdist)
				maxdist = pay;
		}
		for (var tileIndex = 0; tileIndex < distMap.length; tileIndex++){
			pay = distMap[tileIndex].pay;
			distMap[tileIndex] = pay >= 0 ?(1 - (distMap[tileIndex].pay / maxdist)) : pay;
		}
		return distMap;
	},
	
	GetBestTileIndex : function(objectMap, distMap){
		var bestScore = 0;
		var bestTileIndex = -1;
		for (var tileIndex = 0; tileIndex < objectMap.length; tileIndex++){
			var score = 0;
			if (objectMap[tileIndex] >= 0)
				score += objectMap[tileIndex];
			if (distMap[tileIndex] >= 0)
				score += 10 * distMap[tileIndex];
			if (score > bestScore){
				bestScore = score;
				bestTileIndex = tileIndex;
			}
		}
		return bestTileIndex;
	},
};